package DCSource.ConfigManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import DCSource.dcsrc;

public class MGm {
	private int rand(int low, int high) {  
        high++;  
        return (int) (Math.random() * (high - low) + low);  
    }
	private boolean isWindowsSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("windows") >= 0;
	  }
	  
	private boolean isLinuxSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("linux") >= 0;
	  }
	private String umsch(){
		if (isWindowsSystem()==true){
			return String.valueOf((char)13)+String.valueOf((char)10);
		}else{return String.valueOf((char)10);}
	}
	private ArrayList<Manager> list = new ArrayList();
	public MGm(){}
	public MGm(Manager mg){
		list.add(mg);
	}
	public MGm(Manager[] mgs){
		for (int I=0;I<mgs.length;I++){
			list.add(mgs[I]);
		}
	} 
	public void add(Manager mg, boolean overwrite){
		boolean found = false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(mg.getTitle())){
				found = true;
				if (overwrite==true){
					list.set(I, mg);
				}
			}
		}
		if (found==false){
			list.add(mg);
		}
	}
	public void add(Manager[] mgs, boolean overwrite){
		for (int I=0;I<mgs.length;I++){
			Manager mg = mgs[I];
			boolean found = false;
			for (int I2=0;I2<list.size();I2++){
				if (list.get(I2).getTitle().equals(mg.getTitle())){
					found = true;
					if (overwrite==true){
						list.set(I2, mg);
					}
				}
			}
			if (found==false){
				list.add(mg);
			}
		}
	}
	public void rev(Manager mg){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(mg.getTitle())){
				list.remove(I);
				I--;
			}
		}
	}
	public void rev(String mg){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(mg)){
				list.remove(I);
				I--;
			}
		}
	}
	public void rev(int dex){
		if (dex>-1&dex<list.size()){
			list.remove(dex);
		}
	}
	public Manager get(String mg){
		Manager returning=new Manager("");
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(mg)){
				returning = list.get(I);
			}
		}
		return returning;
	}
	public Manager get(Manager mg){
		Manager returning=new Manager("");
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(mg.getTitle())){
				returning = list.get(I);
			}
		}
		return returning;
	}
	public Manager get(int dex){
		Manager returning=new Manager("");
		if (dex>-1&dex<list.size()){
			returning = list.get(dex);
		}
		return returning;
	}
	public Manager[] getall(){
		return list.toArray(new Manager[list.size()]);
	}
	public int count(){
		return list.size();
	}
	public boolean SaveToFile(String uri){
		String Source = getSource();
		try{
			FileOutputStream write = new FileOutputStream(uri);
			write.write(Source.getBytes());
			write.close();
			return true;
		}catch(Exception err){return false;}
	}
	public void loadIn(String url){
		list.clear();
		String returning="";
		try
	    {
	      String link = url;
	      File file=new File(link);
	      FileInputStream inputstream=new FileInputStream(file);
	      InputStreamReader reader=new InputStreamReader(inputstream);
	      BufferedReader br=new BufferedReader(reader);
	      String line;
	      while ((line = br.readLine()) != null) {
	         returning+=line+umsch();
	      }
	    } catch (Exception exp) {}  
		SetSource(returning);
	}
	public String getSource(){
		String Source = "";
		for (int I=0;I<list.size();I++){
			if (I>0){
				Source+=umsch()+";MG;"+umsch()+list.get(I).getSource();
			}else{
				Source+=list.get(I).getSource();
			}
		}
		return Source;
	}
	public void SetSource(String Source){
		list.clear();
		String returning=Source;
		String[] SRC = returning.split(umsch()+";MG;"+umsch());
		for (int I=0;I<SRC.length;I++){
			Manager add = new Manager("ERROR_BY_LOADING");
			add.setSource(SRC[I]);
			if (!add.getTitle().equals("ERROR_BY_LOADING")){
				add(add,true);
			}
		}
	}
	public boolean existManager(Manager mg){
		boolean returning=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(mg.getTitle())){
				returning=true;
			}
		}
		return returning;
	}
	public boolean existManager(String mg){
		boolean returning=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(mg)){
				returning=true;
			}
		}
		return returning;
	}
	public void clear(){
		list.clear();
	}
	public String toString(){
		String text = "";
		for (int I=0;I<list.size();I++){
			text += list.get(I).toString()+";";
		}
		return "{"+text+"}";
	}
}
